/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.web.controller;

import com.opgea.qms.constraints.SessionConstraints;
import com.opgea.qms.domain.model.JsonModelMap;
import com.opgea.qms.domain.model.SessionData;
import com.opgea.qms.domain.model.TokenStatus;
import com.opgea.qms.domain.model.TokenStatusReplyModel;
import com.opgea.qms.service.ApplicationService;
import com.opgea.qms.service.TokenDetailsService;
import com.opgea.qms.service.TokenService;
import com.opgea.qms.web.form.TokenDetailsDTO;
import java.lang.Object;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Ramesh
 */
@Controller
@RequestMapping(value="/token")
public class TokenController {
    
    
    @Autowired
    private ApplicationService applicationService;
    
    @Autowired
    private TokenService tokenService;
    
    @Autowired
    private TokenDetailsService tokenDetailsService;
    
    @RequestMapping(value="waitingTokenList", method=RequestMethod.GET)
    public @ResponseBody Map<String, Object> getWaitingTokenList(@RequestParam(value="companyId", required=false)String compId, 
                                                HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        //List<TokenDetailsDTO> tokenList = tokenDetailsService.findAllByCompanyId(companyId);
        List<TokenStatus> tokenList = applicationService.findAllTokenStatus(companyId);
        return JsonModelMap.success().data(tokenList);
    }
    
    @RequestMapping(value="destinationTokenList", method=RequestMethod.GET)
    public @ResponseBody Map<String, Object> getDestinationTokenList(
                                    @RequestParam(value="companyId", required=false)String compId, 
                                    @RequestParam(value="destinationId", required=false)Long destId, 
                                    HttpServletRequest request){
        
        HttpSession session = request.getSession();
        SessionData sessionData = (SessionData) session.getAttribute(SessionConstraints.SESSION_DATA.name());
        Long companyId = sessionData.getCompanyId();
        Long destinationId = sessionData.getDestinationId();
        if(destId != null){
            destinationId =  destId;
        }
        
        System.out.println("TokenController >> getDesitnationTokenList >>  CompanyId: "+companyId);
        System.out.println("TokenController >> getDesitnationTokenList >>  destinationId: "+destinationId);
        
        List<TokenStatus> tokenList = applicationService.findAllTokenStatusByDestination(companyId, destinationId);
        return JsonModelMap.success().data(tokenList);
    }
    
    @RequestMapping(value="updateToken", method=RequestMethod.POST)
    public @ResponseBody 
            Map<String, Object> updateToken(@RequestParam(required=true, value="tokenId")Long tokenId,
                                            @RequestParam(required=true, value="tokenStatus")Integer tokenStatus,
                                            HttpServletRequest request){
        
        
        TokenDetailsDTO tokenDetailsDTO = tokenDetailsService.findByTokenId(tokenId);
        tokenDetailsDTO.setTokenStatus(tokenStatus);
        System.out.println(tokenDetailsDTO);
        tokenDetailsService.update(tokenDetailsDTO);

        
        return JsonModelMap.success().data(tokenStatus);
    }
    
    @RequestMapping(value="tokenStatusInQueue", method=RequestMethod.GET)
    public @ResponseBody 
            Map<String, Object> tokenStatusInQueue(@RequestParam(required=true, value="orgId")Long organizationId,
                                            @RequestParam(required=true, value="tokenNo")Integer tokenNo,
                                            HttpServletRequest request){
        
        
        TokenStatusReplyModel statusModel = tokenService.tokenReply(organizationId, tokenNo);
        
        
        return JsonModelMap.success().data(statusModel);
        /*
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("Status", replyMessage);
        return dataMap;
         * 
         */
    }
}
